using System.Collections.Generic;
using Microformats.Readers.Interfaces;

namespace Microformats.Readers.Entities
{
    /// <summary>
    /// Holds information about the Microformats that the reader will be searching for.
    /// </summary>
    public class MicroformatInformation : ICommonInformation
    {
        /// <summary>
        /// The attribute in the xml/html that holds the value indicating a microformat is present.
        /// </summary>
        private string rootType;

        /// <summary>
        /// The attribute value in the xml/html that indicates that a microformat is valid.
        /// </summary>
        public string Root;

        /// <summary>
        /// The type of Microformat, hCard, rel-tag etc.
        /// </summary>
        private string name;

        private string dataType;

        private List<ICommonInformation> fields;
        private string plurality;

        private ICommonInformation parentField;

        public MicroformatInformation(ICommonInformation parent)
        {
            parentField = parent;
        }

        /// <summary>
        /// Specifies the fields that are held in this configuration element.
        /// </summary>
        /// <value></value>
        public List<ICommonInformation> Fields
        {
            get { return fields; }
            set { fields = value; }
        }

        /// <summary>
        /// Gets or sets the plurality.
        /// </summary>
        /// <value>The plurality.</value>
        public string Plurality
        {
            get { return plurality; }
            set { plurality = value; }
        }

        /// <summary>
        /// Specifies the datatype of the microformat as a whole.
        /// </summary>
        /// <value></value>
        public string DataType
        {
            get { return dataType; }
            set { dataType = value; }
        }

        /// <summary>
        /// The type of Microformat, hCard, rel-tag etc.
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// Finds the field by name
        /// </summary>
        /// <param name="fieldName">Name of the field.</param>
        /// <returns></returns>
        public ICommonInformation FindFieldByName(string fieldName)
        {
            if(Root == fieldName )
                return this;

            foreach (FieldInformation f in Fields)
            {
                if (f.Name == fieldName)
                    return f;
                //This field has nested configuration so check that first before moving to the next field
                if (f.Fields.Count > 0)
                {
                    foreach (ICommonInformation subFields in f.Fields)
                    {
                        if (subFields.Name == fieldName)
                            return subFields;

                        if (subFields.FindFieldByName(fieldName) != null)
                            return subFields;
                    }
                }
            }

            return null;
        }

        /// <summary>
        /// Gets the parent field.
        /// </summary>
        /// <value>The parent field.</value>
        public ICommonInformation ParentField
        {
            get { return parentField; }
        }

        /// <summary>
        /// The attribute in the xml/html that holds the value indicating a microformat is present.
        /// </summary>
        public string RootType
        {
            get { return rootType; }
            set { rootType = value; }
        }
    }
}